import ArrayUtil from '@/ArrayUtil';
import BuffUtil from '@/BuffUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import { magicDamageConfig } from 'code/Core/System/Configs/DamageConfig';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 萨满-元素之拳
 **/
export default class SM_ElementalFirst extends AbilityBase {
    static instance: SM_ElementalFirst;
    dmgTypes: magicTypes[] = ['fire', 'lighting', 'wind', 'ice'];
    buffType: AppBuffType = {
        id: 'buff_SM_ElementalFirst',
        art: this.art,
        name: this.abilityName,
        ubertip: '攻击时随机附加火焰、冰霜、大地或者闪电伤害',
        dur: 12,
        targetArt: 'Abilities\\Spells\\Items\\AIlb\\AIlbTarget.mdl',
        targetAttach: 'hand,left,right',
        stackCountMax: -1,
    };
    constructor(id: string = 'Aysq') {
        super(id);
        SM_ElementalFirst.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
        EventManager.onTakeAttack(this.onAttackEffect.bind(this));
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        // let buff = BuffUtil.addBuff(source, this.buffType.id, source);
        // buff.stackCount = 4 + this.getAbilityLevel(source);
        BuffCommon.addBuff({
            target: source,
            caster: source,
            buffTypeId: this.buffType.id,
            ablityId: this.id,
            config: { stack: 4 + this.getAbilityLevel(source) },
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    onAttackEffect(d: DamageEventData) {
        let buff = BuffUtil.getUnitBuff(d.source, this.buffType.id, d.source);
        if (!buff) return;
        buff.stackCount -= 1;
        if (buff.stackCount <= 0) buff.destroy();
        let lv = this.getAbilityLevel(d.source);
        let fp = UnitCommon.getFullProperty(d.source);
        let dmg = fp * (0.3 + 0.1 * lv) + UnitCommon.取法强(d.source) * (0.75 + 0.15 * lv);
        DamageSystemInstance.applyDamage(d.target, d.source, dmg, { ...magicDamageConfig, magicType: ArrayUtil.randomElement(this.dmgTypes), abilityId: this.id });
    }
}
